import unittest

from email_template.imp import data_type
from email_template.imp import metadata_mimic


class MetadataMimicTest(unittest.TestCase):

  def test_email_template_validation(self):
    """General EmailTemplate validation suite."""

    # No key value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': None,
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))

    # Invalid type.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': 32,
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))

    # Minimal key length value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))

    # Maximum key length value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': 'AVeryBigKeyThatWillFail',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))

    # Value not alphanumeric, period, or dash only characters value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': 'No/Slash',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))

    # Description greater than 1024 chars value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'x' * 1025,
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.

    # Minimum to length value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': set(),
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.


    # Invalid email address in to value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'bad.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.

    # Invalid email address in cc value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'bad.com'},
                        'subject': 'The subject line for the email.',
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.

    # No subject value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': None,
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': '',
                        'body': 'A body of good text for our good email.',
                      }))  # No to value error.

    # No body value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': '',
                      }))  # No to value error.
    self.assertRaises(ValueError, metadata_mimic.Validate,
                      data_type.EmailTemplate({
                        'key': '10-Valid.Key',
                        'description': 'This is a good email template.',
                        'to': {'someone@xcorp.com', 'manager@xcorp.com'},
                        'cc': {'coworker@xcorp.com'},
                        'subject': 'The subject line for the email.',
                        'body': None,
                      }))  # No to value error.

    # This is the completely valid example.
    good_data = data_type.EmailTemplate({
      'key': '10.Email-Tmpl',
      'description': 'This is a good email template.',
      'to': {'someone@xcorp.com', 'manager@xcorp.com'},
      'cc': {'coworker@xcorp.com'},
      'subject': 'The subject line for the email.',
      'body': 'A body of good text for our good email.',
    })

    try:
      metadata_mimic.Validate(good_data)
    except:
      self.assertFalse(True, 'This call should have succeeded.')


if __name__ == '__main__':
  unittest.main()
